package com.example.controller;


import com.example.bean.LearningHistory;
//import com.example.bean.LoginLog;
import com.example.dao.LearningDao;


//import com.example.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
public class LearningController {

    @Autowired
    private LearningDao learningDao;

//    @Autowired
//    private UserDao Userdao;

    @PostMapping("api/me/studyrecord/generatenewrecord")
    @ResponseBody
    public ResponseEntity<String> createLearningRecord(@RequestBody Map<String, String> payload) {
        String username = payload.get("username");
        String coursename = payload.get("topicname");
        try {
            System.out.println("Starting to write learning history for user "+username +" and course " +coursename);
            Integer userid = learningDao.getUserIdByUserName(username);
            System.out.println("user id found:"+userid.toString());


            Long courseid = learningDao.findCourseIdByContent(coursename);
            System.out.println("course id found:"+courseid.toString());

            Integer courseexp = learningDao.getExpByCourseId(courseid);

            // 插入学习记录，增加经验
            learningDao.insertLearningRecord(userid, courseid);
            learningDao.levelUpForCourse(username,courseexp);

            return ResponseEntity.ok("学习记录生成成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("学习记录生成失败: 后端在" + e.getMessage());
        }
    }


    @PostMapping("api/me/studyrecord/newpractice")
    @ResponseBody
    public ResponseEntity<String> createPracticeRecord(@RequestBody Map<String, String> payload) {
        String username = payload.get("username");
        String coursename = payload.get("topicname");
        try {
            System.out.println("Starting to write practice history for user "+username +" and course " +coursename);
            Integer userid = learningDao.getUserIdByUserName(username);
            System.out.println("user id found:"+userid.toString());


            Long courseid = learningDao.findCourseIdByContent(coursename);
            System.out.println("course id found:"+courseid.toString());

            Integer courseexp = learningDao.getExpByCourseId(courseid);

            // 插入学习记录，增加经验
            learningDao.insertLearningRecord(userid, courseid);
            learningDao.levelUpForCourse(username,courseexp);

            return ResponseEntity.ok("练习记录生成成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("练习记录生成失败: 后端在" + e.getMessage());
        }
    }

    @GetMapping("api/me/studyrecord/history")
    public ResponseEntity<List<LearningHistory>> getLearningHistoryByUserName(@RequestParam String username) {
        try {
            // 使用LearningDao中的方法，通过用户名获取用户ID
            Integer userId = learningDao.getUserIdByUserName(username);
            if (userId == null) {
                // 如果没有找到用户，返回404状态码
                return ResponseEntity.notFound().build();
            }
            // 使用LearningDao中的方法，通过用户ID获取学习记录
            List<LearningHistory> learningHistory = learningDao.findLearningHistoryByUserId(userId);
            // 如果没有学习记录，返回404状态码
            if (learningHistory == null || learningHistory.isEmpty()) {
                List<LearningHistory> emptyList = new ArrayList<>();
                return ResponseEntity.ok(emptyList);
            }
            // 成功返回学习记录
            return ResponseEntity.ok(learningHistory);
        } catch (Exception e) {
            // 出现异常时，返回500状态码
            return ResponseEntity.internalServerError().build();
        }
    }


    @GetMapping("api/me/studyrecord/Practicehistory")
    public ResponseEntity<List<LearningHistory>> getPracticeHistoryByUserName(@RequestParam String username) {
        try {
            // 使用LearningDao中的方法，通过用户名获取用户ID
            Integer userId = learningDao.getUserIdByUserName(username);
            if (userId == null) {
                // 如果没有找到用户，返回404状态码
                return ResponseEntity.notFound().build();
            }
            // 使用LearningDao中的方法，通过用户ID获取学习记录
            List<LearningHistory> learningHistory = learningDao.findPracticeHistoryByUserId(userId);
            // 如果没有学习记录，返回404状态码
            if (learningHistory == null || learningHistory.isEmpty()) {
                List<LearningHistory> emptyList = new ArrayList<>();
                return ResponseEntity.ok(emptyList);
            }
            // 成功返回学习记录
            return ResponseEntity.ok(learningHistory);
        } catch (Exception e) {
            // 出现异常时，返回500状态码
            return ResponseEntity.internalServerError().build();
        }
    }

    @GetMapping("api/me/studyrecord/countalgorithm")
    public ResponseEntity<Integer> countAlgorithmNo(@RequestParam String username) {
        Integer userId = learningDao.getUserIdByUserName(username);
        Integer count = learningDao.countAlgorithmNo(userId);
        return ResponseEntity.ok(count);
    }

    @GetMapping("api/me/studyrecord/countcourse")
    public ResponseEntity<Integer> countCourseNo(@RequestParam String username) {
        Integer userId = learningDao.getUserIdByUserName(username);
        Integer count = learningDao.countCourseNo(userId);
        return ResponseEntity.ok(count);
    }

    @GetMapping("api/me/studyrecord/getID")
    public ResponseEntity<Integer> getUserID(@RequestParam String username) {
        Integer userId = learningDao.getUserIdByUserName(username);
        return ResponseEntity.ok(userId);
    }

}
